using Game.Main.Model;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class WateringPotItem : BaseItem
	{
		private const float TIME_SUM = 2f;

		private Collider2D targetObject;

		private BaseItem targetItem;

		private float stayTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (!isDrag)
			{
				return;
			}
			RaycastHit2D[] array = Physics2D.BoxCastAll(base.transform.position + new Vector3(-0.666f, -0.331f), new Vector2(0.644f, 1.387f), 0f, Vector2.zero, 15f, 512);
			if (array.Length > 0)
			{
				for (int i = 0; i < array.Length - 1; i++)
				{
					for (int num = array.Length - 1; num > i; num--)
					{
						if (array[num - 1].collider.transform.position.z > array[num].collider.transform.position.z)
						{
							RaycastHit2D raycastHit2D = array[num];
							array[num] = array[num - 1];
							array[num - 1] = raycastHit2D;
						}
					}
				}
			}
			bool flag = false;
			for (int j = 0; j < array.Length; j++)
			{
				RaycastHit2D raycastHit2D2 = array[j];
				if (!(raycastHit2D2.collider != null) || (!raycastHit2D2.collider.transform.parent.name.Contains("Flowerpot") && !raycastHit2D2.collider.transform.parent.name.Contains("Mound")))
				{
					continue;
				}
				BaseItem component = raycastHit2D2.collider.transform.parent.GetComponent<BaseItem>();
				if ((component.itemData.scriptType == 104 || component.itemData.scriptType == 146) && (component.itemData.scriptType != 104 || ((FlowerpotItem)component).CanGrowUp()) && (component.itemData.scriptType != 146 || ((MoundItem)component).CanGrowUp()))
				{
					if (targetObject == null || targetObject != raycastHit2D2.collider)
					{
						targetObject = raycastHit2D2.collider;
						targetItem = component;
						stayTimer = 0f;
					}
					flag = true;
					break;
				}
			}
			if (targetObject != null && !flag)
			{
				ResetTarget();
			}
			if (!(targetItem != null) || !(stayTimer < 2f))
			{
				return;
			}
			stayTimer += Time.deltaTime;
			if (stayTimer >= 2f)
			{
				if (targetItem.itemData.scriptType == 104)
				{
					((FlowerpotItem)targetItem).GrowUp();
				}
				else if (targetItem.itemData.scriptType == 146)
				{
					((MoundItem)targetItem).GrowUp();
				}
				stayTimer = 0f;
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			model.Find("DragEfc").SetObjectActive(true);
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			model.Find("DragEfc").SetObjectActive(false);
			ResetTarget();
		}

		private void ResetTarget()
		{
			targetObject = null;
			targetItem = null;
			stayTimer = 0f;
		}
	}
}
